package com.hdu.other.dp.shuweiDp;

public class lc233 {
    class Solution {
        public int countDigitOne(int n) {
            return cal(n, 1);
        }

        int cal(int n, int d) {
            int ans = 0;
            for (int left, cur, right = 1, tmp = n; tmp != 0; tmp /= 10, right *= 10) {
                left = tmp / 10;
                cur = tmp % 10;
                if (d == 0) left--;
                ans += left * right;
                if (cur > d) ans += right;
                else if (cur == d) ans += n % right + 1;
            }
            return ans;
        }
    }
}
